package com.voicechat.firstmet.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.adapter.PhotoAdapter;
import com.voicechat.firstmet.utils.StatusBarUtils;
import com.voicechat.firstmet.utils.Utils;

import java.util.ArrayList;

/**
 * @class name：com.je.activity_imessage
 * @class 查看大图
 * @Description
 * @anthor ljp
 * @time 2017/4/1 0001 下午 03:00
 */
public class PictureViewerActivity extends FragmentActivity {
    private static final String STATE_POSITION = "STATE_POSITION";
    private TextView tvPosition;
    private TextView tvSize;
    private int position1;
    private Bundle list;
    private ArrayList<String> tlist;
    private RelativeLayout back;
    private RelativeLayout relaSave;
    private String TAG = "PictureViewerActivity";
    private int width, height, statusBarHeight;
    private ViewPager hackyViewPager;//图片展示容器
    private ProgressBar progressBar;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_picture_viewer);
        initView(savedInstanceState);
        StatusBarUtils.Full(this);
    }
    PhotoAdapter mAdapter;
    private void initView(Bundle savedInstanceState) {

        Intent intent = getIntent();
        position1 = intent.getIntExtra("position",1);
        list = intent.getBundleExtra("list");
        tlist = list.getStringArrayList("tlist");

        width = Utils.getSrecchWidth(this);
        height = Utils.getScreenHeight(this);
        statusBarHeight = Utils.getStatusBarHeight(this);

        progressBar = (ProgressBar) findViewById(R.id.activity_picture_viewer_progressbar);
        tvPosition = (TextView)findViewById(R.id.tv_picture_position);
        tvSize = (TextView)findViewById(R.id.tv_picture_size);
        hackyViewPager = (ViewPager)findViewById(R.id.vp_picture_viewer);
        back = (RelativeLayout) findViewById(R.id.image_picture_back);
        tvPosition.setText(position1+1+"");
        tvSize.setText(tlist.size()+"");

        mAdapter = new PhotoAdapter(PictureViewerActivity.this,tlist,progressBar);//todo 传入progressbar
        hackyViewPager.setAdapter(mAdapter);
        mAdapter.addOnPageClickListener(new PhotoAdapter.OnPageClickListener() {
            @Override
            public void onClick(View view, int position) {
                finish();
            }

           // @Override
           /* public void onLongClick(View view, int position) {
                View view1 = LayoutInflater.from(PictureViewerActivity.this).inflate(R.layout.pop_picture, null);
                RelativeLayout viewById1 = (RelativeLayout) view1.findViewById(R.id.picture_rela);
                TextView viewById2 = (TextView) view1.findViewById(R.id.tv_save);
                viewById1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUtils.hidePopupWindow();
                    }
                });
                viewById2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        final int currentItem = hackyViewPager.getCurrentItem();
                        Log.i(TAG, "onClick: ----"+currentItem);
                        final String picNameFromPath = ImageUtils.getPicNameFromPath(tlist.get(currentItem));
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                   *//* //因为盆友圈gif不能以bitmap保存，所以保存走数据流的格式
                                        ImageUtils.getImage(HTTPAPI.CIRCLE_PIC+tlist.get(currentItem),ConstantSet.PICTUREPICTURE,picNameFromPath);
                                        //发一个系统广播通知手机有图片更新
                                        File myCaptureFile = new File(ConstantSet.PICTUREPICTURE+picNameFromPath);
                                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                        Uri uri = Uri.fromFile(myCaptureFile);
                                        intent.setData(uri);
                                        sendBroadcast(intent);*//*
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }).start();
                        PopUtils.hidePopupWindow();
                        Toast.makeText(PictureViewerActivity.this, "已保存至"+ ConstantSet.PICTUREPICTURE, Toast.LENGTH_SHORT).show();
                    }
                });
                PopUtils.showPopupWindow(PictureViewerActivity.this, view, view1, width, height - statusBarHeight, 0, statusBarHeight, true,false);
            }*/
        });

        hackyViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tvPosition.setText(position+1+"");

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        if (savedInstanceState != null) {
            position1 = savedInstanceState.getInt(STATE_POSITION);
        }
        hackyViewPager.setCurrentItem(position1);

      /*  relaSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                View view1 = LayoutInflater.from(PictureViewerActivity.this).inflate(R.layout.pop_picture, null);
                RelativeLayout viewById1 = (RelativeLayout) view1.findViewById(R.id.picture_rela);
                TextView viewById2 = (TextView) view1.findViewById(R.id.tv_save);
                viewById1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        PopUtils.hidePopupWindow();
                    }
                });
                viewById2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                                final int currentItem = hackyViewPager.getCurrentItem();
                                Log.i(TAG, "onClick: ----"+currentItem);
                                final String picNameFromPath = ImageUtils.getPicNameFromPath(tlist.get(currentItem));
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        //因为盆友圈gif不能以bitmap保存，所以保存走数据流的格式
                                       *//* ImageUtils.getImage(HTTPAPI.CIRCLE_PIC+tlist.get(currentItem),ConstantSet.PICTUREPICTURE,picNameFromPath);
                                        //发一个系统广播通知手机有图片更新
                                        File myCaptureFile = new File(ConstantSet.PICTUREPICTURE+picNameFromPath);
                                        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                                        Uri uri = Uri.fromFile(myCaptureFile);
                                        intent.setData(uri);
                                        sendBroadcast(intent);*//*
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            }).start();
                        PopUtils.hidePopupWindow();
                        Toast.makeText(PictureViewerActivity.this, "已保存至"+ ConstantSet.PICTUREPICTURE, Toast.LENGTH_SHORT).show();
                    }
                });
                PopUtils.showPopupWindow(PictureViewerActivity.this, view, view1, width, height - statusBarHeight, 0, statusBarHeight, true,false);
            }
        });*/

    }




    public void onSetBack(View view) {
        finish();
    }

}
